package com.smartstate.dataManagement.mapper;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartstate.dataManagement.dto.RealTimeDTO;
import com.smartstate.dataManagement.entity.Equipment;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface EquipmentMapper extends BaseMapper<Equipment> {
    /**
     * 根据设备编码判断该编码是否存在
     *
     * @param deviceId
     * @return
     */
    @Select("SELECT COUNT(`deviceId`) FROM `equipment` WHERE `deviceId` = #{deviceId} ")
    Integer getByCode(String deviceId);

    @Select("SELECT * FROM `equipment` WHERE `deviceId` = #{deviceId} ")
    Equipment getOne(String deviceId);

    @Update("UPDATE equipment SET additionalAttribute = #{additionalAttribute} WHERE deviceId = #{deviceId} ")
    boolean updateByDeviceId(Equipment equipment);


    @Select("select e.id,e.name,e.deviceId from equipment as e where e.type= #{type} ")
    List<Equipment> selectEquipment(String type);


    @Select("select  * from  equipment   where deviceId=#{code}")
    Equipment getEquimentByCode(@Param("code") String code);

    @MapKey("id")
    @Select("select id, deviceId from equipment where type = (select type from equipment_type where name = #{name})")
    List<Map<String, Object>> queryDeviceId(@Param("name") String name);

    /**
     * 删除设备的时候把报警信息也删除
     * @param deviceId
     */
    @Delete("delete from alarm where deviceId = #{deviceId}")
    void deleteAlarm(String deviceId);

    @Select("select * from equipment")
    List<Equipment> findAll();

    //过十分钟，判断一下设备有没有修改或者新增
    //  SELECT * FROM `equipment` WHERE (createTime> "2022-09-09 15:58:16" AND createTime <"2022-09-09 17:02:56")  OR (updateTime > "2022-09-09 15:56:47" AND updateTime <"2022-09-09 17:40:42")
    @Select("select * from equipment where (updateTime > #{tenTimeAgo} and updateTime < #{nowTime}) or (createTime > #{tenTimeAgo} and createTime < #{nowTime}) ")
    List<Equipment> findByTenTimeAgo(DateTime nowTime, DateTime tenTimeAgo);

    @Select("SELECT COUNT(`name`) FROM `equipment` WHERE `name` = #{name} ")
    Integer getByName(String name);

    @Select("SELECT * FROM equipment where  type = 1")
    List<Equipment> selectEquipmentType();

    @Select("SELECT * FROM equipment where  type = #{deviceType}")
    List<Equipment> selectEquipmentByType(Integer deviceType);

    List<Equipment> selectEquipmentByTypes(@Param("dataDTO") RealTimeDTO dataDTO);
}
